<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="券码" prop="couponCode">
        <el-input
          v-model="queryParams.couponCode"
          placeholder="请输入券码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发放的用户" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入发放的用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="卡号" prop="couponCard">
        <el-input
          v-model="queryParams.couponCard"
          placeholder="请输入卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="对应的券名" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入对应的券名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-if="couponType != 3"
          v-hasPermi="['cancel:couponCode:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleGenCode"
          v-if="couponCodeList.length > 0 && couponCodeList[0].genType == 1"
          v-hasPermi="['cancel:coupon:genCode']"
          >生成券码</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <keep-alive>
      <coupon-virtual
        :couponCodeList="couponCodeList"
        :total="total"
        :queryParams="queryParams"
        :loading="loading"
        v-if="couponType == 3"
      ></coupon-virtual>
      <coupon-detail
        :couponCodeList="couponCodeList"
        :total="total"
        :queryParams="queryParams"
        :couponType="couponType"
        :loading="loading"
        @change="handleDelCode"
        @form="handleForm"
        v-else
      ></coupon-detail>
    </keep-alive>

    <!-- 添加或修改券码列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="发放数量" prop="dcNum">
          <!-- <el-input v-model="form.sendNum" placeholder="请输入发放数量" /> -->
          <el-input-number v-model="form.dcNum" :min="1"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="生成券码" :visible.sync="isGenCode" width="800px">
      <el-form ref="codeForm" :model="codeForm" label-width="100px">
        <el-form-item label="券码名称" prop="couponName">
          <el-input
            v-model="codeForm.couponName"
            disabled
            placeholder="请输入券码名称"
          />
        </el-form-item>
        <el-form-item label="券码金额" prop="couponPrice">
          <el-input
            v-model="codeForm.couponPrice"
            disabled
            placeholder="请输入券码金额"
          />
        </el-form-item>
        <el-form-item label="发放数量" prop="genNum">
          <!-- <el-input v-model="form.sendNum" placeholder="请输入发放数量" /> -->
          <el-input-number
            v-model="codeForm.genNum"
            :min="1"
            :max="codeForm.sendNum"
          ></el-input-number>
        </el-form-item>
      </el-form>

      <div slot="footer">
        <el-button @click="isGenCode = false">取 消</el-button>
        <el-button :loading="genLoad" type="primary" @click="subGenCode"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCouponCode } from "@/api/system/couponCode";
import CouponDetail from "../../components/couponDetail/index.vue";
import CouponVirtual from "../../components/couponDetail/virtual.vue";
import { genCode } from "@/api/system/coupon";
export default {
  name: "CouponCode",
  data() {
    return {
      genLoad: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 券码列表表格数据
      couponCodeList: [{}],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      couponType: "",
      isGenCode: false,
      //生成券码
      codeForm: {},
    };
  },
  created() {
    this.getList();
    this.couponType = this.$route.query.couponType;
  },
  components: {
    CouponDetail,
    CouponVirtual,
  },
  methods: {
    /** 查询券码列表列表 */
    getList() {
      this.loading = true;
      console.log(this.$route.query);
      this.queryParams.cid = this.$route.query.cid;
      listCouponCode(this.queryParams).then((response) => {
        this.couponCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dcNum: null,
      };
      this.resetForm("form");
    },
    //作废
    handleDelCode(res) {
      if (res.code == 200) {
        this.getList();
      }
    },

    handleForm(res) {
      if (res.code == 200) {
        this.getList();
      }
    },
    /**生成券码 */
    handleGenCode() {
      const {
        cid,
        couponName,
        couponPrice,
        couponMakeType,
        couponLen,
        typeWay,
        outTime,
      } = this.couponCodeList[0];
      this.isGenCode = true;

      this.codeForm = {
        ...this.codeForm,
        id: cid,
        couponName,
        couponPrice,
        couponMakeType,
        couponLen,
        typeWay,
        useEndTime: outTime,
        genNum: 1,
      };
    },
    // 提交生成券码
    subGenCode() {
      this.genLoad = true;
      this.$refs["codeForm"].validate((valid) => {
        if (valid) {
          this.codeForm.sendNum = this.codeForm.genNum;
          genCode(this.codeForm)
            .then((res) => {
              this.$modal.msgSuccess("生成成功");
              this.isGenCode = false;
              this.genLoad = false;
              this.getList();
            })
            .catch((err) => {
              this.genLoad = false;
            });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.download(
            "cancel/couponCode/export",
            {
              ...this.form,
              cid: this.$route.query.cid,
            },
            `couponCode_${new Date().getTime()}.xlsx`
          ).then(() => {
            this.open = false;
          });
        }
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "cancel/couponCode/export",
        {
          ...this.form,
          cid: this.$route.query.cid,
        },
        `couponCode_${new Date().getTime()}.xlsx`
      ).then(() => {
        this.open = false;
      });
    },
  },
};
</script>
